package com.example.voucher.service.impl;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.voucher.contract.VoucherItemCapable;
import com.example.voucher.contract.spi.VoucherItemCapablePurchaseInvoiceIvdAdapter;
import com.example.voucher.entity.InvoiceVerifyDetail;
import com.example.voucher.entity.PurchaseInvoice;
import com.example.voucher.extension.ExtensionImpl;
import com.example.voucher.mapper.InvoiceVerifyDetailMapper;
import com.example.voucher.mapper.PurchaseInvoiceMapper;
import com.example.voucher.service.PurchaseInvoiceQueryService;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

@ExtensionImpl(extendFrom = PurchaseInvoiceQueryService.class)
public class DefaultPurchaseInvoiceQueryServiceIvdImpl implements PurchaseInvoiceQueryService {

    @Autowired
    private InvoiceVerifyDetailMapper ivdMapper;
    @Autowired
    private PurchaseInvoiceMapper invoiceMapper;

    @Override
    public List<VoucherItemCapable> queryVoucherItem(List<Long> pkidList) {
        // 组装VoucherItemCapable
        List<VoucherItemCapable> list = Lists.newArrayList();

        // 查询InvoiceVerifyDetail
        LambdaQueryWrapper<InvoiceVerifyDetail> querySnWrapper = Wrappers.<InvoiceVerifyDetail>lambdaQuery();
        querySnWrapper.in(InvoiceVerifyDetail::getInvoiceId, pkidList);
        List<InvoiceVerifyDetail> ivdList = ivdMapper.selectList(querySnWrapper);

        // 查询IncomeInvoice
        List<PurchaseInvoice> invoiceList = invoiceMapper.selectBatchIds(pkidList);

        Map<Long, PurchaseInvoice> invoiceMap =
            invoiceList.stream().collect(Collectors.toMap(PurchaseInvoice::getId, Function.identity()));

        Map<String, String> itIcMap = Maps.newHashMap();
        itIcMap.put("xw三级盘螺1", "三级盘螺");
        itIcMap.put("xw三级抗震1", "三级抗震螺纹钢");
        itIcMap.put("xw四级螺纹钢2", "四级螺纹钢");
        for (InvoiceVerifyDetail ivd : ivdList) {
            PurchaseInvoice invoice = invoiceMap.get(ivd.getInvoiceId());
            list.add(new VoucherItemCapablePurchaseInvoiceIvdAdapter(ivd, invoice, itIcMap));
        }

        return list;
    }

}
